package com.earl.eatbao.web;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.earl.eatbao.commons.util.GsonUtil;
import com.earl.eatbao.commons.vo.ResultMessage;
import com.earl.eatbao.domain.eatbao.GoodsVo;
import com.earl.eatbao.domain.eatbao.Orders;
import com.earl.eatbao.service.OrdersService;
import com.google.common.reflect.TypeToken;
import com.pingplusplus.model.Charge;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

/**
 * @author Administrator
 * @version 1.0
 * @created 23-十一月-2016 16:55:14
 */
@RestController
@RequestMapping("/orders")
public class OrdersController extends BaseController {

	private final Logger logger = LoggerFactory.getLogger(OrdersController.class);

	@Autowired
	private OrdersService ordersService;

	/**
	 * 查看商家所有订单
	 *
	 * @param userId
	 *            商家对应的账号id 就是通常说的用户id
	 * @return
	 */
	@ApiOperation(value = "查看商家所有订单", notes = "查看商家所有订单", httpMethod = "GET", response = String.class)
	@RequestMapping(value = "/shop/{userId}", method = RequestMethod.GET)
	public ResponseEntity<ResultMessage> findShopAllOrders(
			@ApiParam(name = "userId", value = "根据商家ID获取商家商店点单") @PathVariable Long userId) {
		logger.info("<===== 进入findShopAllOrders:方法,[userId]" + userId);
		ResultMessage result = new ResultMessage();
		result.setMessageCode(200l);
		result.getResultParm().put("orderList", ordersService.findShopAllOrders(userId));
		result.setResultInfo("获取商家订单数据成功");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 查看商家所有订单
	 *
	 * @param userId
	 *            商家对应的账号id 就是通常说的用户id
	 * @return
	 */
	@ApiOperation(value = "查看用户所有订单", notes = "查看用户所有订单", httpMethod = "GET", response = String.class)
	@RequestMapping(value = "/user/{userId}", method = RequestMethod.GET)
	public ResponseEntity<ResultMessage> findUserAllOrders(
			@ApiParam(name = "userId", value = "根据商家ID获取用户点单") @PathVariable Long userId) {
		logger.info("<===== 进入findShopAllOrders:方法,[userId]" + userId + "======");
		ResultMessage result = new ResultMessage();
		result.setMessageCode(200l);
		result.getResultParm().put("orderList", ordersService.findUserAllOrders(userId));
		if (null != result.getResultParm().get("orderList")) {
			result.setResultInfo("请求操作成功");
		} else {
			result.setResultInfo("请求操作失败");
		}

		logger.info("==== 退出findUserAllOrders方法 返回值类型:{ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 用户下单
	 *
	 * @param orders
	 *            订单实体
	 * @return
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "totalPrice", value = "订单总价数量", required = true, dataType = "float", paramType = "query"),
			@ApiImplicitParam(name = "userId", value = "用户Id", required = true, dataType = "Long", paramType = "query"),
			@ApiImplicitParam(name = "payType", value = "支付类型1.在线 2.线下", required = true, dataType = "", paramType = "query"),
			@ApiImplicitParam(name = "shopUserId", value = "商家用户ID", required = true, dataType = "Long", paramType = "query") })
	@ApiOperation(value = "用户下单", notes = "用户下单", httpMethod = "POST", response = String.class)
	@RequestMapping(value = "/placeOrder", method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> placeOrder(Orders orders,
			@ApiParam(name = "goodsList", value = "订单项聚合信息，包含数量，商品id") @RequestParam(name = "goodsList") String goodsList,
			@ApiParam(name = "addressId", required = true, value = "地址ID") @RequestParam(name = "addressId", required = true) Long addressId) {
		logger.info("<====== 进入placeOrder:方法,{[orders] :" + orders.toString() + ":" + goodsList.toString() + "} ===");
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		// 解析Gson数据
		List<GoodsVo> goodsVoList = GsonUtil.fromJson(goodsList, new TypeToken<List<GoodsVo>>() {
		}.getType());
		result.setServiceResult(ordersService.placeOrder(orders, goodsVoList, addressId));
		result.setMessageCode(200l);
		result.getResultParm().put("ordersId", orders.getId());
		if (result.getServiceResult()) {
			result.setResultInfo("请求操作成功");
		} else {
			result.setResultInfo("请求操作失败");
		}

		logger.info("==== 退出placeOrder方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 商家接单
	 *
	 * @param ordersId
	 * @return
	 */
	@ApiOperation(value = "商家接单", notes = "商家接单", httpMethod = "POST", response = String.class)
	@RequestMapping(value = "/accept/{ordersId}", method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> acceptOrders(
			@ApiParam(name = "ordersId", value = "订单ID", required = true) @PathVariable Long ordersId) {
		logger.info("<====== 进入acceptOrders:方法,{[ordersId] :" + ordersId + " } ====");
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.setServiceResult(ordersService.acceptOrders(ordersId));
		result.setMessageCode(200l);
		if (result.getServiceResult()) {
			result.setResultInfo("请求操作成功");
		} else {
			result.setResultInfo("请求操作失败");
		}

		logger.info("==== 退出acceptOrders方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 拒绝接单
	 * 
	 * @param ordersId
	 * @return
	 */
	@ApiOperation(value = "拒绝接单", notes = "拒绝接单", httpMethod = "POST", response = String.class)
	@RequestMapping(value = "/refuse/{ordersId}", method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> refuseOrders(
			@ApiParam(name = "ordersId", value = "商家提交的拒绝订单ID", required = true) @PathVariable Long ordersId) {

		logger.info("<====== 进入refuseOrders:方法,{[ordersId] :" + ordersId + " } ====");
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.setServiceResult(ordersService.refuseOrders(ordersId));
		result.setMessageCode(200l);
		if (result.getServiceResult()) {
			result.setResultInfo("请求操作成功");
		} else {
			result.setResultInfo("请求操作失败");
		}

		logger.info("==== 退出refuseOrders方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 商家发货
	 * 
	 * @param ordersId
	 * @return
	 */
	@ApiOperation(value = "商家发货", notes = "商家发货", httpMethod = "POST", response = String.class)
	@RequestMapping(value = "/deliver/{ordersId}", method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> deliverOrders(
			@ApiParam(name = "ordersId", value = "商家提交的接订单ID", required = true) @PathVariable Long ordersId) {
		logger.info("<====== 进入deliverOrders:方法,{[ordersId] :" + ordersId + " } ====");

		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.setServiceResult(ordersService.deliverOrders(ordersId));
		result.setMessageCode(200l);
		if (result.getServiceResult()) {
			result.setResultInfo("请求操作成功");
		} else {
			result.setResultInfo("请求操作失败");
		}

		logger.info("==== 退出deliverOrders方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 用户申请退单
	 *
	 * @return ordersId 订单编号
	 */
	@ApiOperation(value = "用户申请退单", notes = "用户申请退单", httpMethod = "POST", response = String.class)
	@RequestMapping(value = "/reback/{ordersId}", method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> rebackOrders(
			@ApiParam(name = "ordersId", value = "用户申请退单订单ID", required = true) @PathVariable Long ordersId) {

		logger.info("<====== 进入rebackOrders:方法,{[ordersId] :" + ordersId + " } ====");
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.setServiceResult(ordersService.rebackOrders(ordersId));
		result.setMessageCode(200l);
		if (result.getServiceResult()) {
			result.setResultInfo("请求操作成功");
		} else {
			result.setResultInfo("请求操作失败");
		}

		logger.info("==== 退出rebackOrders方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 用户完成订单
	 * 
	 * @param ordersId
	 * @return
	 */
	@ApiOperation(value = "用户完成订单", notes = "用户完成订单", httpMethod = "POST", response = String.class)
	@RequestMapping(value = "/done/{ordersId}", method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> doneOrders(
			@ApiParam(name = "ordersId", value = "用户确认收货提交的单ID", required = true) @PathVariable Long ordersId) {
		logger.info("<====== 进入doneOrders:方法,{[ordersId] :" + ordersId + " } ====");
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.setServiceResult(ordersService.doneOrders(ordersId));
		result.setMessageCode(200l);
		if (result.getServiceResult()) {
			result.setResultInfo("请求操作成功");
		} else {
			result.setResultInfo("请求操作失败");
		}
		logger.info("==== 退出doneOrders方法 返回值类型:{ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 获取所有订单
	 *
	 * @return
	 */
	@ApiOperation(value = "获取所用订单", notes = "获取所用订单", httpMethod = "GET", response = String.class)
	@RequestMapping(value = "/all", method = RequestMethod.GET)
	public ResponseEntity<ResultMessage> getAllOrders() {
		logger.info("<====== 进入getAllOrders:方法 ===");
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.getResultParm().put("ordersList", ordersService.findAll());
		result.setMessageCode(200l);
		if (null != result.getResultParm().get("ordersList")) {
			result.setResultInfo("请求操作成功");
		} else {
			result.setResultInfo("请求操作失败");
		}

		logger.info("==== 退出getAllOrders方法 返回值类型: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 获取指定订单状态的订单
	 * 
	 * @param status
	 * @return
	 */

	@ApiOperation(value = "获取指定订单状态的订单", notes = "获取指定订单状态的订单", httpMethod = "GET", response = String.class)
	@RequestMapping(value = "/findPointStatus", method = RequestMethod.GET)
	public ResponseEntity<ResultMessage> findOrdersByStatus(
	        @ApiParam(name = "status", value = "订单状态", required = true)
            @RequestParam(name = "status", required = true) Integer status) {

		logger.info("<====== 进入findOrdersByStatus:方法,{[status] :" + status + " } ====");
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.getResultParm().put("orderList", ordersService.findOrdersByStatus(status));
		result.setMessageCode(200l);
		if (null != result.getResultParm().get("orderList")) {
			result.setResultInfo("请求操作成功");
		} else {
			result.setResultInfo("请求操作失败");
		}

		logger.info("==== 退出findOrdersByStatus方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 查找订单详情
	 * 
	 * @param ordersId
	 * @return
	 */
	@ApiOperation(value = "查询订单详情", notes = "查询订单详情", httpMethod = "GET", response = String.class)
	@RequestMapping(value = "/ordersInfo/{ordersId}", method = RequestMethod.GET)
	public ResponseEntity<ResultMessage> getOrdersInfo(
			@ApiParam(name = "ordersId", value = "订单ID") @PathVariable Long ordersId) {

		logger.info("<====== 进入getOrdersInfo:方法,{[ordersId] :" + ordersId + " } ====");
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.getResultParm().put("orderDetailList", ordersService.getOrdersInfo(ordersId));
		result.getResultParm().put("order", ordersService.findOne(ordersId));
		result.setMessageCode(200l);
		if (null != result.getResultParm().get("orderDetailList")) {
			result.setResultInfo("请求操作成功");
		} else {
			result.setResultInfo("请求操作失败");
		}

		logger.info("==== 退出getOrdersInfo方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 获取所有订单 
	 * @return
	 */
	@ApiOperation(value = "查看商家未接订单", notes = "查看商家未接订单", httpMethod = "GET", response = String.class)
	@RequestMapping(value = "/shop/unwatch/{userId}", method = RequestMethod.GET)
	public ResponseEntity<ResultMessage> findShopUnWatchOrder(@PathVariable Long userId) {
		logger.info("<====== 进入getAllOrders:方法 ===");
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.getResultParm().put("ordersList", ordersService.findShopUnWatchOrder(userId));
		result.setMessageCode(200l);
		if (null != result.getResultParm().get("ordersList")) {
			result.setResultInfo("请求操作成功");
		} else {
			result.setResultInfo("请求操作失败");
		}
		logger.info("==== 退出getAllOrders方法 返回值类型: } =======>");

		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 获取所有订单 
	 * @return
	 */
	@ApiOperation(value = "查看商家未发货订单", notes = "查看商家未发货订单", httpMethod = "GET", response = String.class)
	@RequestMapping(value = "/shop/unsend/{shopperId}", method = RequestMethod.GET)
	public ResponseEntity<ResultMessage> findShopUnsendOrder(@PathVariable Long shopperId) {
		logger.info("<====== 进入getAllOrders:方法 ===");
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.getResultParm().put("ordersList", ordersService.findShopUnSendOrder(shopperId));
		result.setMessageCode(200l);
		if (null != result.getResultParm().get("ordersList")) {
			result.setResultInfo("请求操作成功");
		} else {
			result.setResultInfo("请求操作失败");
		}
		logger.info("==== 退出getAllOrders方法 返回值类型: } =======>");
		
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 获取所有订单 
	 * @return
	 */
	@ApiOperation(value = "支付订单", notes = "支付订单", httpMethod = "POST", response = String.class)
	@RequestMapping(value = "/pay", method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> payOrders(
			@ApiParam(name = "ordersId", required = true, value = "订单Id")
			@RequestParam(name = "ordersId", required = true) Long ordersId,
			@ApiParam(name = "payWay", required = true, value = "支付方式")
			@RequestParam(name = "payWay", required = true)String payWay) {
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();

		Charge charge = ordersService.payOrders(ordersId, payWay);
		if (null != charge) {
			result.setResultInfo("请求操作成功");
			result.setServiceResult(true);
			result.getResultParm().put("charge", charge);
		} else {
			result.setResultInfo("请求操作失败");
			result.setServiceResult(false);
		}
		result.setMessageCode(200l);
		
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 支付回调接口
	 * @param orderId
	 * @return
	 */
	@ApiOperation(value = "支付成功回调接口", notes = "支付成功回调接口", httpMethod = "POST", response = String.class)
	@RequestMapping(value="/callBackPay",  method = RequestMethod.POST)
    public ResponseEntity<ResultMessage> callBackPay(@RequestParam(name = "orderId") Long orderId) {
	    // desc: 封装操作结果
	    ResultMessage result = new ResultMessage();
	    result.setServiceResult(ordersService.callBackPayOrders(orderId));
	    result.setMessageCode(200l);
	    if (result.getServiceResult()) {
	        result.setResultInfo("请求操作成功");
	    } else {
	        result.setResultInfo("请求操作失败");
	    }

	    logger.info("==== 退出callBackPay方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
	    return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
    }

	/**
	 * 获取用户未收货订单
	 * @return
	 */
	@ApiOperation(value = "用户未收货订单", notes = "用户未收货订单", httpMethod = "GET", response = String.class)
	@RequestMapping(value = "/user/unget/{userId}", method = RequestMethod.GET)
	public ResponseEntity<ResultMessage> findUserUnGetOrders(@PathVariable Long userId) {
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.getResultParm().put("ordersList", ordersService.findUserUnGetOrders(userId));
		result.setServiceResult(true);
		result.setMessageCode(200l);
		
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 获取用户未评论订单
	 * @return
	 */
	@ApiOperation(value = "获取用户未评论订单", notes = "获取用户为评论订单", httpMethod = "GET", response = String.class)
	@RequestMapping(value = "/user/uncomment/{userId}", method = RequestMethod.GET)
	public ResponseEntity<ResultMessage> findUserUnCommentOrders(@PathVariable Long userId) {
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.getResultParm().put("ordersList", ordersService.findUserUnCommentOrders(userId));
		result.setServiceResult(true);
		result.setMessageCode(200l);
		
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 获取用户未评论订单
	 * @return
	 */
	@ApiOperation(value = "用户确认收货", notes = "用户确认收货", httpMethod = "POST", response = String.class)
	@RequestMapping(value = "/confirm/{ordersId}", method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> userConfirmGetOrders(@PathVariable Long ordersId) {
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		ordersService.userConfirmGetOrders(ordersId);
		result.setServiceResult(true);
		result.setMessageCode(200l);
		
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 获取用户未评论订单
	 * @return
	 */
	@ApiOperation(value = "获取用户未支付订单", notes = "获取用户未支付订单", httpMethod = "GET", response = String.class)
	@RequestMapping(value = "/user/unpay/{userId}", method = RequestMethod.GET)
	public ResponseEntity<ResultMessage> userUnpayOrders(@PathVariable Long userId) {
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.getResultParm().put("ordersList", ordersService.userUnpayOrders(userId));
		result.setServiceResult(true);
		result.setMessageCode(200l);
		
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

}